//
//  JFMainController.swift
//  iOS_Swift_WeiBo
//
//  Created by huang on 16/3/19.
//  Copyright © 2016年 huang. All rights reserved.
//

import UIKit

class JFMainController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        addChildViewControllers()
        
        // 添加加号按钮（自定义tabBar,添加加号按钮,系统的tabBar是只读的.不能直接使用=赋值,使用KVC来赋值 tabBar = CZMainTabBar()）
        self.setValue(JFMainTabBar(), forKey: "tabBar")
    }
    
    // 添加子控制器
    func addChildViewControllers() {
        // 设置首页
        let homeVC = JFHomeController()
        addChildViewController(homeVC, title: "首页", imageName: "tabbar_home")
        
        // 设置消息
        let messageVC = JFMessageController()
        addChildViewController(messageVC, title: "消息", imageName: "tabbar_message_center")
        
        // 设置发现
        let discoverVC = JFDiscoverController()
        addChildViewController(discoverVC, title: "发现", imageName: "tabbar_discover")
        
        // 设置我
        let profileVC = JFProfileController()
        addChildViewController(profileVC, title: "我", imageName: "tabbar_profile")
    }
    
    // 为方便添加子控制器而写的方法
    func addChildViewController(controller:UIViewController, title:String, imageName:String) {
        // 设置标题
        controller.title = title
        
        // 设置普通图片
        controller.tabBarItem.image = UIImage(named: imageName)
        
        // 设置高亮图片
        let hightedImageName = imageName + "_highlighted"
        controller.tabBarItem.selectedImage = UIImage(named: hightedImageName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        // 设置item文字的颜色
        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: UIControlState.Selected)
        
        // 添加到子控制器
        addChildViewController(UINavigationController(rootViewController: controller))
    }
}
